package com.tz.yiqingapp.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tz.yiqingapp.bean.Hosptial;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.AdminMapper; 
import com.tz.yiqingapp.mapper.HosptialMapper;
import com.tz.yiqingapp.mapper.UserMapper;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminHosptial {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    HosptialMapper hosptialMapper;

    @Autowired
    UserMapper studentMapper;

    @AccessLimit(times = 5)
    @PostMapping("/searchHosptial")
    // @ApiOperation(value = "查询Hosptial", notes = "查询Hosptial名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> searchHosptial(@RequestBody Hosptial hosptial, HttpServletRequest request) {

        if(request.getSession().getAttribute("loginrole") == null ||
            !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<Hosptial> hosptialQueryWrapper = new QueryWrapper<>();
        hosptialQueryWrapper.or().lambda().like(Hosptial::getName,hosptial.getName());
        List<Hosptial> hosptialList = hosptialMapper.selectList(hosptialQueryWrapper);
        return ResponseUtils.success(hosptialList);
    }

    @AccessLimit(times = 5)
    @PostMapping("/getHosptialList")
    // @ApiOperation(value = "查询Hosptial", notes = "查询Hosptial名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> getHosptialList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        Page<Hosptial> hosptialPage = new Page<>(pageDTO.getCurrentpage() , pageDTO.getPagesize());
        IPage<Hosptial> hosptialIPage = hosptialMapper.selectPage(hosptialPage , null);
        return ResponseUtils.successlist(  hosptialIPage.getTotal(),hosptialIPage.getPages(),hosptialIPage.getCurrent(),hosptialIPage.getRecords());
    }
    @AccessLimit(times = 5)
    @PostMapping("/getAllHosptial")
    // @ApiOperation(value = "查询Hosptial", notes = "查询Hosptial名称", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> getAllHosptial(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        List<Hosptial> hosptials = hosptialMapper.selectList(null);
        return ResponseUtils.success(hosptials);
    }
    @AccessLimit(times = 5)
    @PostMapping("/addHosptial")
    // @ApiOperation(value = "添加Hosptial", notes = "软删除Hosptial参数", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> addHosptial(@RequestBody Hosptial hosptial, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<Hosptial> hosptialQueryWrapper = new QueryWrapper<>();
        hosptialQueryWrapper.or().lambda().eq(Hosptial::getName,hosptial.getName());
        List<Hosptial> hosptialList = hosptialMapper.selectList(hosptialQueryWrapper);
        if(hosptialList.size() >0) {
            return ResponseUtils.fail("hosptial exits");
        }
        hosptialMapper.insert(hosptial);
        return ResponseUtils.success("hosptial exits");
    }


    @AccessLimit(times = 5)
    @PostMapping("/updateHosptial")
    // @ApiOperation(value = "修改Hosptial信息", notes = "修改Hosptial信息", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> updateHosptial(@RequestBody Hosptial hosptial, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        hosptialMapper.updateById(hosptial);
        return ResponseUtils.success(hosptial);
    }

    @AccessLimit(times = 5)
    @PostMapping("/delHosptial")
    // @ApiOperation(value = "软删除Hosptialid", notes = "软删除Hosptial", httpMethod = "POST")
    @ResponseBody
    public HashMap<String, Object> delHosptial(@RequestBody Hosptial delhosptial, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("admin")){
            return ResponseUtils.notlogin("not login");
        }
        delhosptial.setStatus(1);
        hosptialMapper.updateById(delhosptial);
        return ResponseUtils.fail("hosptial delete fail");

    }
}
